//
//  ZviewDownlist.m
//  NetbarToplin
//
//  Created by 55like on 21/02/2017.
//  Copyright © 2017 55like. All rights reserved.
//
#import "ZviewDownlist.h"

@interface ZviewDownlist()
{
}
@end
@implementation ZviewDownlist

-(void)showlist{
    {
        UIScrollView*sv=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frameWidth, self.frameHeight)];
        
        [self addSubview:sv];
        {
            UIView*view1=[[UIView alloc]initWithFrame:sv.bounds];
            [sv addSubview:view1];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
            // 连续敲击2次,手势才能识别成功
            tap.numberOfTapsRequired = 1;
            tap.numberOfTouchesRequired = 1;
            // 2.添加手势识别器对象到对应的view
            [view1 addGestureRecognizer:tap];
            // 3.添加监听方法(识别到了对应的手势,就会调用监听方法)
            [tap addTarget:self action:@selector(close)];
        }
        
        
        NSArray*array=[self.parameterDic objectForKey:@"titlearray"];
        
        for (int i=0; i<array.count; i++) {
            NSDictionary*dic=array[i];
            NSString*title;
            if ([dic isKindOfClass:[NSString class]]) {
                title=(NSString*)dic;
            }else{
          title  =[dic getSafeObjWithkey:@"title"];
            }
            
            UIView*view1=[RHMethods viewWithFrame:CGRectMake(0, sv.contentHeight, sv.frameWidth, 40) backgroundcolor:zrgbwhiteColor superView:sv];
              __weak __typeof(self) weakSelf = self;
            [view1 addViewClickBlock:^(UIView *view) {
                if (weakSelf.allcallBlock) {
                    
                    if ([dic isKindOfClass:[NSString class]]) {
                        
                        weakSelf.allcallBlock(@{@"title":title},200,nil);
                    }else{
                    weakSelf.allcallBlock(dic,200,nil);
                    }
                }
                [weakSelf close];
                
            }];
            sv.contentHeight=view1.frameYH;

            [RHMethods viewWithFrame:CGRectMake(10, view1.frameHeight-0.5, kScreenWidth-20, 0.5) backgroundcolor:RGBCOLOR(228, 228, 228) superView:view1];
            //左边lable
            UILabel*lb1=[RHMethods lableX:10 Y:10 W:view1.frameWidth-2*10 Height:15 font:15 superview:view1 withColor:nil text:title];
            [lb1 beCY];
            
            if ([self.parameterDic objectForKey:@"currentdata"]) {
                NSDictionary*currentdic=[self.parameterDic objectForKey:@"currentdata"];
                if ([[currentdic getSafeObjWithkey:@"id"] isEqualToString:[dic getSafeObjWithkey:@"id"] ]) {
                    lb1.textColor=rgb(239,186,58);
                }
//                if ([[currentdic getSafeObjWithkey:@"ZJid"] isEqualToString:[dic getSafeObjWithkey:@"ZJid"] ]) {
//                    lb1.textColor=ORGZT;
//                }
                
            }else{
                if ([title isEqualToString:[self.parameterDic objectForKey:@"currenttitle"]]) {
                    lb1.textColor=rgb(239,186,58);
                }
            }
           
            
        }
        
        
        
       sv.frameHeight=self.frameHeight;
        kScrollViewCouldScroll(sv);
        
    }
    
}
-(void)openme{
    BaseViewController*vc;
    if ([self.parameterDic isKindOfClass:[NSDictionary class]]) {
        vc= [self.parameterDic objectForKey:@"me"];
    }
    if(vc==nil){
        vc=UTILITY.currentViewController;
        
    }else if ([vc isKindOfClass:[BaseViewController class]]) {
        
    }else if ([vc isKindOfClass:[UIView class]]) {
        
        //            UIView*view=vc;
        vc=[vc performSelector:@selector(viewController)];
    }
    if ([self.parameterDic objectForKey:@"y"]) {
        float myy=[[self.parameterDic objectForKey:@"y"] floatValue];
        
        
        self.frame=CGRectMake(0, myy, kScreenWidth, vc.view.frameHeight-myy);
    }else{
        self.frame=CGRectMake(0, kTopHeight+40, kScreenWidth, vc.view.frameHeight-kTopHeight-40);
    }
    
    self.backgroundColor=RGBACOLOR(22, 22, 22, 0.3);
//    self.frame=CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    [self showlist];
    [vc.view addSubview:self];
    
}
-(void)close{
    [self removeFromSuperview];
    if (self.allcallBlock) {
        self.allcallBlock(@{@"title":[self.parameterDic objectForKey:@"currenttitle"]},201,nil);
    }
}
+(ZviewDownlist*)showMeWithDic:(NSMutableDictionary*)parameterdic CallBackBlock:(AllcallBlock) callBackBlock{
    ZviewDownlist*view=[UTILITY.currentViewController getAddValueForKey:@"ZviewDownlist"];
    if (!view) {
        view=[[ZviewDownlist alloc]initWithFrame:CGRectMake(0, kTopHeight+40, kScreenWidth, kScreenHeight-kTopHeight-40)];
        view.parameterDic=parameterdic;
        [UTILITY.currentViewController.view addSubview:view];
//        [UTILITY.currentViewController setAddValue:view forKey:@"ZviewDownlist"];
        [view openme];
    }
    view.allcallBlock=callBackBlock;
    view.hidden=NO;
    
    
    return view;

}
@end
